package org.ezplatform.office.rd.psyj.controller;

import org.ezplatform.core.common.WebUser;
import org.ezplatform.core.service.BaseService;
import org.ezplatform.core.web.controller.BaseController;
import org.ezplatform.core.web.util.WebUtils;
import org.ezplatform.core.web.view.OperationResult;

import org.ezplatform.office.rd.psyj.dao.LogDao;
import org.ezplatform.office.rd.psyj.entity.LogEntity;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping({"/api/w/psyj"})
public class PsyjController {
    @Resource
    private JdbcTemplate jdbcTemplate;


    @Autowired
    private LogDao logDao;


    @GetMapping({"/getList"})
    public OperationResult getList(String id)
    {
        String sql = "select distinct task_def_key,task_def_name from BPM_PROCESS_COMMENT where processinstance_id=? ";
        List list = this.jdbcTemplate.queryForList(sql, new Object[] { id });
        return OperationResult.buildSuccessResult(list);
    }

    @GetMapping({"/addYj"})
    public OperationResult addYj(String id, String content, String time, String userids, String key,String keyName) {
        if (!"".equals(userids)) {
            String name = "";
            String[] arr = userids.split(",");
            if ((arr != null) && (arr.length > 0))
            {
                for (String userid : arr) {
                    String sql = "select user_name from sys_user where id=?";
                    String username = (String)this.jdbcTemplate.queryForObject(sql, new Object[] { userid }, String.class);
                    String sql1 = "select org_id from sys_user where id=?";
                    String org_id = (String)this.jdbcTemplate.queryForObject(sql1, new Object[] { userid }, String.class);
                    name = name + username;

                    String selSql = "select type,org_showtype,task_def_name,field,inscription_show from BPM_PROCESS_COMMENT where processinstance_id=? and task_def_key=?";

                    String type = "";
                    String org_showtype = "";
                    String task_def_name = "";
                    String field = "";
                    String inscription_show = "";
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                    LocalDateTime dateTime = LocalDateTime.parse(time, formatter);

                    List li = this.jdbcTemplate.queryForList(selSql, new Object[] { id, key });

                    if ((li != null) && (li.size() > 0)) {
                        Map map = (Map)li.get(0);
                        type = map.get("TYPE") + "";
                        org_showtype = map.get("ORG_SHOWTYPE") + "";
                        task_def_name = map.get("TASK_DEF_NAME") + "";
                        field = map.get("FIELD") + "";
                        inscription_show = map.get("INSCRIPTION_SHOW") + "";
                    }
                    String insql = "insert into BPM_PROCESS_COMMENT(ID,CREATEDBY,CREATEDDATE,CREATEDORG,LASTMODIFIEDBY,LASTMODIFIEDDATE,LASTMODIFIEDORG,USER_ID,CONTENT,TASK_ID,TYPE,SIGNTIME,READSCOPE,CORP_ID,PROCESSINSTANCE_ID,SIGNTYPE,REPLACEID,ORG_SHOWTYPE,CANREPLY,USER_NAME,REPLACENAME,TASK_DEF_KEY,TASK_DEF_NAME,COMMENT_SOURCE,LOG_ID,COMMENT_FILE_ID,FIELD,comment_attitude,inscription_show,optlock) values (?, ?,  ?, ?, null, null, null,   ?, ?, '', ?, ?, null,    '0', ?, '', null, ?, null, ?, null, ?,     ?, '1', '402881147e89d75d017ed2be253d2641', null, ?,     null, ?, 0);";

                    String uid = UUID.randomUUID().toString().replace("-", "").toLowerCase();

                    this.jdbcTemplate.update(insql, new Object[] { uid, userid, dateTime, org_id, userid, content, type, dateTime, id, org_showtype, username, key, task_def_name, field, inscription_show });

                    //保存领导补办得操作日志
                    saveLog(id, key, time, name,keyName,userids,org_id);
                }

            }


        } else {
            return OperationResult.buildSuccessResult("用户为空！");
        }
        return OperationResult.buildSuccessResult("操作成功！");
    }

//保存领导补办得操作日志
    public void saveLog(String processInstanceId,String key,String time,String name,String keyName,String userids,String org_id){
        UUID uuid = UUID.randomUUID();
        String uuid32 = uuid.toString().replace("-", "").toLowerCase();
        String sql = "  INSERT INTO bpm_process_log (\"id\",\"createdby\",\"createddate\",\"createdorg\",\"lastmodifiedby\",\"lastmodifieddate\",\"lastmodifiedorg\",\"optlock\",\"curactivityid\",\"curactivityname\",\n" +
                "\"nextactivityid\",\"nextactivityname\",\"dealaction\",\"dealactionname\",\"dealuserid\",\"dealusername\",\"receivenames\",\"receiveids\",\"process_instance_id\",\"taskid\",\"create_taskid\",\n" +
                "\"delete_taskid\",\"update_taskid\",\"deal_time\",\"corp_id\",\"operation_details\",\"show_type\",\"next_taskid\",\"cancel_reason\",\"comment_id\",\"client_type\") VALUES\n" +
                "\t ('"+uuid32+"','"+userids+"',NOW(),'"+org_id+"',NULL,NULL,NULL,0,\n" +
                "\t '"+key+"','"+keyName+"','','','','补办','"+userids+"','"+name+"','',\n" +
                "\t '','"+processInstanceId+"','',NULL,NULL,NULL,NOW(),'0',NULL,NULL,NULL,NULL,'','');  ";
        jdbcTemplate.update(sql);
    }


}
